using Mapster;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using QiAdmin.Core;
using QiAdmin.Core.Services.Log;
using QiAdmin.Core.Services.Token;
using QiAdmin.Entity;
using QiAdmin.Model.Dtos;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Model.Dtos.Output;
using QiAdmin.Repository.Repository;
using QiAdmin.Service.IService;
using ICacheService = QiAdmin.Core.Services.Cache.ICacheService;

namespace QiAdmin.Service.Service
{
    /// <summary>
    /// TSysRoleMenu Services
    /// </summary>
    public class TSysRoleMenuService : BaseService<TSysRoleMenu, TSysRoleMenu_IN, TSysRoleMenu_OUT>, ITSysRoleMenuService
    {
        readonly TSysRoleMenuRepository _tSysRoleMenuRepository;
        public TSysRoleMenuService(ILogService logService,
            ITokenService tokenService, ICacheService cacheService, IHttpContextAccessor httpContextAccessor, TSysRoleMenuRepository tSysRoleMenuRepository) : base(logService, tokenService, cacheService, httpContextAccessor.HttpContext)
        {
            _tSysRoleMenuRepository= tSysRoleMenuRepository;
        }

        #region 增删改查-自动生成

        /// <summary>
        /// 创建默认条件
        /// </summary>
        /// <returns></returns>
        public Expressionable<TSysRoleMenu> CreateExp()
        {
            var exp = Expressionable.Create<TSysRoleMenu>();

            return exp;
        }

        /// <summary>
        /// 新增单条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Add(TSysRoleMenu_IN input)
        {
            var model = input.Adapt<TSysRoleMenu>();
            int Id = _tSysRoleMenuRepository.InsertReturnIdentity(model);
            return CommonResult.Convert(Id > 0);
        }

        /// <summary>
        /// 新增多条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult AddRange(List<TSysRoleMenu_IN> input)
        {
            var list = input.Adapt<List<TSysRoleMenu>>();
            var result = _tSysRoleMenuRepository.InsertRange(list);
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Edit(TSysRoleMenu_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);
            var model = input.Adapt<TSysRoleMenu>();
            var result = _tSysRoleMenuRepository.UpdateAndIgnore(model, exp.ToExpression());
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 获取单条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult<TSysRoleMenu_OUT> Get(TSysRoleMenu_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);

            var result = _tSysRoleMenuRepository.GetFirst(exp.ToExpression()).Adapt<TSysRoleMenu_OUT>();
            return new CommonResult<TSysRoleMenu_OUT>(result);
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult<PageOutput<TSysRoleMenu_OUT>> GetPageList(TSysRoleMenu_IN input)
        {
            var exp = CreateExp();

            exp.AndIF(input.StartQueryTime != DateTime.MinValue, t => t.SysCreateTime >= input.StartQueryTime.Date);
            exp.AndIF(input.EndQueryTime != DateTime.MinValue, t => t.SysCreateTime < input.EndQueryTime.Date.AddDays(1));

            PageOutput<TSysRoleMenu_OUT> pageOutput = new PageOutput<TSysRoleMenu_OUT>();
            PageModel pageModel = new() { PageIndex = input.PageIndex, PageSize = input.PageSize };
            pageOutput.Data = _tSysRoleMenuRepository.GetPageList(exp.ToExpression(), pageModel).Adapt<List<TSysRoleMenu_OUT>>();
            pageOutput.DataCount = pageModel.TotalCount;

            return new CommonResult<PageOutput<TSysRoleMenu_OUT>>(pageOutput);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult Remove(TSysRoleMenu_IN input)
        {
            var exp = CreateExp();
            exp.And(t => t.Id == input.Id);

            var result = _tSysRoleMenuRepository.Delete(exp.ToExpression());
            return CommonResult.Convert(result);
        }

        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public CommonResult RemoveRange(List<TSysRoleMenu_IN> input)
        {
            var exp = CreateExp();
            exp.And(t => input.Select(s => s.Id).Contains(t.Id));

            var result = _tSysRoleMenuRepository.Delete(exp.ToExpression());
            return CommonResult.Convert(result);
        }

        #endregion

        public CommonResult<List<TSysRoleMenu_OUT>> GetRoleMenuCache()
        {
            var list = _CacheService.Get<List<TSysRoleMenu_OUT>>(App.StaticData.CacheKey_RoleMenu);
            return new CommonResult<List<TSysRoleMenu_OUT>>(list);
        }

        public CommonResult AddRoleMenuCache()
        {
            var roleData = _tSysRoleMenuRepository.Context.Queryable<TSysRoleMenu>().LeftJoin<TSysMenu>((rm, m) => rm.MenuId == m.Id).Select((rm, m) => new TSysRoleMenu_OUT
            {
                MenuId = rm.MenuId,
                RoleId = rm.RoleId,
                MenuPermission = m.Permission,
                MenuApiTag=m.ApiTag,
            }).ToList();
            _CacheService.Add(App.StaticData.CacheKey_RoleMenu, roleData.Adapt<List<TSysRoleMenu_OUT>>());
            return CommonResult.Success;
        }

    }
}
